package com.wwl.springcloud.controller;

import cn.hutool.json.JSONUtil;
import com.wwl.springcloud.dto.CommonResult;
import com.wwl.springcloud.dto.Payment;
import com.wwl.springcloud.lb.LoadBalancer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/8/5 15:02
 * @description:
 */
@Slf4j
@RestController
@Api(value = "/consumer",description = "支付", tags = {"consumer"})
public class OrderController {
    //public static final String PRODUCT_URL = "http://localhost:8001"; //单机版指定地址
    public static final String PRODUCT_URL = "http://CLOUD-PAYMENT-SERVICE";//集群只指定服务名

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private LoadBalancer loadBalancer;
    @Resource
    private DiscoveryClient discoveryClient;
    @PostMapping("/create")
    @ApiOperation("创建列表")
    public CommonResult<Payment> create(@RequestBody Payment payment){
        return restTemplate.postForObject(PRODUCT_URL+"/payment/save",payment,CommonResult.class);
    }
    @PostMapping("/find")
    @ApiOperation("查询列表")
    public CommonResult<Payment> getPayment(@ApiParam(value = "id") @RequestParam(value = "id",required = false) Integer id){
        Map<String,Object> map = new HashMap();
        map.put("id",id);
        return restTemplate.postForObject(PRODUCT_URL+"/payment/find",map,CommonResult.class);
    }

    @PostMapping("/findByEntity")
    @ApiOperation("查询列表")
    public CommonResult<Payment>findByEntity(@ApiParam(value = "id") @RequestParam(value = "id",required = false) Integer id){
        Map<String,Object> map = new HashMap();
        map.put("id",id);
        ResponseEntity<CommonResult> entity = restTemplate.postForEntity(PRODUCT_URL+"/payment/find",map,CommonResult.class);
        log.info("返回结果:{}", JSONUtil.toJsonStr(entity));
        if (entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }else {
            return CommonResult.error("操作失败!");
        }
    }
    @ApiOperation("测试轮询负载均衡")
    @GetMapping(value = "/consumer/payment/lb")
    public String getPaymentLB(){
        List<ServiceInstance> instanceList = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instanceList == null || instanceList.size() <= 0){
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instance(instanceList);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri+"/payment/payment/lb",String.class);
    }
    // ====================> zipkin+sleuth
    @ApiOperation("payment/zipkin")
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }


}
